import request from '@/utils/request';
import { RecordsResponseData, AddOrUpdateRole, MenuResponseData } from './type'
enum API {
    // 获取角色分页列表
    ROLELIST_URL = "/admin/acl/role/",
    // 添加角色
    ADDROLE_URL = "/admin/acl/role/save",
    // 修改角色
    UPDATEROLE_URL = "/admin/acl/role/update",
    // 根据角色获取对应权限
    ALLPERMISSION = "/admin/acl/permission/toAssign/",
    // 提交角色权限
    ASSIGNPERMISSION = "/admin/acl/permission/doAssign/?",
    // 删除一个角色
    DELETEROLE_URL = "/admin/acl/user/remove/",
    // 删除一组角色
    DELETEROLEGROUP_URL = "/admin/acl/user/batchRemove",
}
// 获取角色分页列表
export const reqGetRoleList = (page: number, limit: number, keyWord: string) => request.get<any, RecordsResponseData>(API.ROLELIST_URL + `${page}/${limit}/?roleName=${keyWord}`)
// 新增或者修改角色
export const reqAddOrUpdateRole = (data: AddOrUpdateRole) => {
    if (data.id) {
        return request.put<any, any>(API.UPDATEROLE_URL, data)
    } else {
        return request.post<any, any>(API.ADDROLE_URL, data)
    }
}
// 根据角色获取对应权限
export const reqAllPermission = (id: number) => request.get<any, MenuResponseData>(API.ALLPERMISSION + id)
// 提交角色权限
export const reqSetPermission = (roleId: number, permissionId: number[]) => request.post<any, any>(API.ASSIGNPERMISSION + `roleId=${roleId}&permissionId=${permissionId}`)
// 删除一组角色或者一个角色
export const reqDelOneOrGroupRole = (ids: number[]) => {
    if (ids.length > 1) {
        return request.delete<any, any>(API.DELETEROLEGROUP_URL, {
            data: ids
        })
    } else {
        return request.delete<any, any>(API.DELETEROLE_URL + ids[0])
    }
}